package com.shop.mall.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

/**
 * 连接数据源
 *
 * @author admin
 */
public class DBFactory {

    private static DBFactory single = null;

    public static Map<String, DruidDataSource> map = new HashMap<>();


    public DBFactory() {

    }

    /**
     * 获取实例
     *
     * @return
     */
    public static DBFactory getInstance() {
        if (single == null) {
            synchronized (DBFactory.class) {
                if (single == null) {
                    single = new DBFactory();
                }
            }
        }
        return single;
    }

    public Connection getConnection(String id) throws SQLException {
        DruidDataSource source = map.get(id);
        return source.getConnection();
    }

    public void addDataBaseIn(Properties prop) {
        if ("Mysql".equalsIgnoreCase(prop.getProperty("type"))) {
            prop.setProperty("driverClassName", "com.mysql.jdbc.Driver");
            prop.setProperty("validationQuery", "SELECT 1 FROM DUAL");
            prop.setProperty("url", "jdbc:mysql://" + prop.getProperty("ip") + ":" + prop.getProperty("port") + "/" + prop.getProperty("database"));
            prop.setProperty("connectionProperties", "useUnicode=true;characterEncoding=UTF8");
        } else if ("Oracle".equalsIgnoreCase(prop.getProperty("type"))) {
            prop.setProperty("driverClassName", "oracle.jdbc.driver.OracleDriver");
            prop.setProperty("validationQuery", "SELECT 1 FROM DUAL");
            prop.setProperty("url",
                    "jdbc:oracle:thin:@" + prop.getProperty("ip") + ":" + prop.getProperty("port") + ":" + prop.getProperty("database"));
        } else if ("Hive".equalsIgnoreCase(prop.getProperty("type"))) {
            prop.setProperty("driverClassName", "org.apache.hive.jdbc.HiveDriver");
            prop.setProperty("validationQuery", "SELECT 1");
            prop.setProperty("url", "jdbc:hive2://" + prop.getProperty("ip") + ":" + prop.getProperty("port") + "/" + prop.getProperty("database"));
        } else {
            throw new RuntimeException("连接池目前只支持Mysql、Oracle、Hive三种数据库类型！");
        }
        try {
            DruidDataSource druidDataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(prop);
            map.put(prop.getProperty("id"), druidDataSource);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("新增数据库创建连接池失败！");
        }
    }

    public void removeDataBaseOut(String id) {
        DruidDataSource source = map.get(id);
        source.close();
        map.remove(id);
    }

    public boolean containsId(String id) {
        return map.containsKey(id);
    }

    public static void release(Connection conn, PreparedStatement pstmt, ResultSet rs) {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (pstmt != null) {
            try {
                pstmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }
}
